//  药品选择列表
//  TabletNameTableViewController.m
//  yilutongxing
//
//  Created by ios-21 on 17/4/16.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "TabletNameTableViewController.h"
#import "MedicinesModel.h"

#define URL_Getmedicinies @"/api/medicines?page=0&size=100000"

#define INPUT_TAG 666
#define BTN_TAG 888

@interface TabletNameTableViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UITextField *nameTF;
@property (strong, nonatomic) IBOutlet UIButton *addBtn;
@property(nonatomic,strong)NSArray *allMedicines;
@property(nonatomic,strong)YZEDBHelper *dbHelper;
@end

@implementation TabletNameTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"药品名称";
    
    self.dbHelper = [YZEDBHelper sharedInstace];
    [self.dbHelper setDBFilePath:@"cachedb" dbName:DBName_Default];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_addBtn addTarget:self action:@selector(OnAddMedicineClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:URL_Getmedicinies] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        NSMutableArray *medicinesArr =[MedicinesModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
        self.allMedicines = medicinesArr;
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allMedicines.count+1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    UITextField *tf = [cell viewWithTag:INPUT_TAG];
    if (tf) {
        [tf removeFromSuperview];
    }
    UIButton *btn = [cell viewWithTag:BTN_TAG];
    if (btn) {
        [btn removeFromSuperview];
    }
    if (indexPath.row<self.allMedicines.count) {
        MedicinesModel *model=self.allMedicines[indexPath.row];
        cell.textLabel.text = model.name;
    }
    else { //其它药品，用户输入
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row>=self.allMedicines.count) {
        return;
    }
    MedicinesModel *model = self.allMedicines[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(backMedicineName:index:)]) {
        [self.delegate backMedicineName:model index:self.index];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 事件响应

-(void)OnAddMedicineClick:(UIButton*)sender{
    if (self.nameTF.text.length==0) {
        return;
    }
    MedicinesModel *model = [MedicinesModel new];
    model.id =  0;
    model.name = self.nameTF.text;
    
    if ([self.delegate respondsToSelector:@selector(backMedicineName:index:)]) {
        [self.delegate backMedicineName:model index:self.index];
    }
    [self.navigationController popViewControllerAnimated:YES];
}


@end
